from launch import LaunchDescription
from launch_ros.actions import Node
from launch_ros.parameter_descriptions import ParameterValue
from launch.substitutions import Command,LaunchConfiguration
from launch.actions import DeclareLaunchArgument
from ament_index_python.packages import get_package_share_directory
import os
def generate_launch_description():
    urdf_path=get_package_share_directory('cpp06_urdf')
    urdf_path=os.path.join(urdf_path,*['urdf','urdf','demo01_helloworld.urdf'])
    model=DeclareLaunchArgument(name='model',default_value=urdf_path)
    p_value=ParameterValue(
        Command(['xacro ',LaunchConfiguration("model")])
    )
    robot_state_pub=Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        parameters=[{'robot_description':p_value}]
    ) 
    # joint_state_pub=Node(
    #     package='joint_state_publisher',
    #     executable='joint_state_publisher'
    #     #,
    #     # parameters=[{'robot_description':p_value}]
    # ) 
    
    urdf_file=get_package_share_directory('cpp06_urdf')
    urdf_file=os.path.join(urdf_file,*['rviz','urdf.rviz'])
    rviz2=Node(package="rviz2",executable="rviz2",
               arguments=['-d',urdf_file])
    
    #return LaunchDescription([model,robot_state_pub,joint_state_pub,rviz2])
    return LaunchDescription([model,robot_state_pub,rviz2])

#  ros2 launch cpp06_urdf display_launch.py model:=`ros2 pkg prefix --share cpp06_urdf`/urdf/urdf/demo02_helloworld.urdf

# joint_state_publisher_gui 让关节运行到指定位置之后，关节会存在抖动的现象
# 在初始位置和指定位置之间抖动 joint_state_publisher_gui 又启动了 joint_state_pub
# 这两个节点重复了
# 解决办法 joint_state_pub 注释掉
#